package com.apes.hr.transfer.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;

import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.ApesConst;

import com.apes.framework.util.DateUtil;
import com.apes.hr.base.model.*;
import com.apes.scm.masterdata.frame.model.Branch;
import com.apes.scm.masterdata.party.model.PartyRole;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;
import com.apes.scm.masterdata.enumeration.model.Enumeration;


import javax.persistence.*;
import java.util.*;

/**
 * 功能：调动申请 明细
 * @author FengJiaJing
 * @create 2018-07-03 11:20
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = TransferApplicationItem.CONST_TABLE_NAME)
public class TransferApplicationItem extends Model {
    public static final String CONST_TABLE_NAME = "hr_transfer_application_item";

    @Id
    @Column(name = "id")
    @ApesGenerator(code = CONST_TABLE_NAME, name = "调动单明细号", padding = 15)
    @Comment("编码")
    private Integer id; //编码

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "transfer_uid", referencedColumnName = "id")
    @Comment("人员编码")
    private Employee transferUid; //人员编码

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "branch_id", referencedColumnName = "id")
    @Comment("部门")
    private Branch branch; // 部门

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "company_id", referencedColumnName = "id")
    @Comment("公司")
    private PartyRole company; //公司

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "company_id_after", referencedColumnName = "id")
    @Comment("调动后公司")
    private PartyRole companyAfter; //公司

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "branch_id_after", referencedColumnName = "id")
    @Comment("调动后部门")
    private Branch branchAfter; // 调动后部门

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "job_id", referencedColumnName = "id")
    @Comment("职位")
    private Job job; //职位

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "job_id_after", referencedColumnName = "id")
    @Comment("调动后职位")
    private Job jobAfter; // 调动后职位

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "post_id", referencedColumnName = "id")
    @Comment("调动前岗位")
    private WorkPost workPost; // 调动前岗位

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "post_id_after", referencedColumnName = "id")
    @Comment("调动后岗位")
    private WorkPost workPostAfter; //  调动后岗位

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "rank_id", referencedColumnName = "id")
    @Comment("职级")
    private Rank rankId; // 职级

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "rank_id_after", referencedColumnName = "id")
    @Comment("调动后职级")
    private Rank rankIdAfter; //调动后职级

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "on_duty_id", referencedColumnName = "id")
    @Comment("在岗状态")
    private Enumeration onDutyState; //在岗状态:(0:试用期;1:代理期;2:待岗期;3:实习..

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "on_duty_id_after", referencedColumnName = "id")
    @Comment("调动后在岗状态")
    private Enumeration onDutyStateAfter; // 调动后在岗状态:(0:试用期;1:代理期;2:待岗期;3:实习期;4:转正;5:长假;9:支援期)

//    @ManyToOne(fetch = FetchType.LAZY)
//    @JoinColumn(name = "person_group", referencedColumnName = "id")
//    private PersonGroup personGroup; // 人员分组(1:店长;2:储备店长;3:服务顾问;4:储备服务顾问-社招;5:储备服务顾问-校招;6:助理;7:美容主管;8:美容组员工;9:机电主管;10:机电组员工;11:钣金主管;12:钣金组员工;13:喷漆主管;14:喷漆组员工;15:门卫;16:调度司机;17:残联;18:临时工-含暑假工\装修小工\劳务工;19:董事长;20:副总裁;21:总经理;22:副总经理;23:总监;24:经理;25:职能主管;26:职能主管级以下;27:小时工;28:助理区长;29:波箱王;30:兼职;31:副总监;32:职能副经理;33:职能副主管)
//
//    @ManyToOne(fetch = FetchType.LAZY)
//    @JoinColumn(name = "person_group_after", referencedColumnName = "id")
//    private PersonGroup personGroupAfter;

    @Column(name = "staff_share",  nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4)
    @Comment("份数")
    private double staffShare = 0; //份数 份额

    @Column(name = "staff_share_after", nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4)
    @Comment("调整后份数")
    private double staffShareAfter = 0; // 调整后份数 份额

    @Column(name = "driver")
    @Comment("是否准驾人员")
    private boolean driver; // 是否准驾人员(0:否;1:是)

    @Column(name = "echelon")
    @Comment("是否梯队")
    private boolean echelon; //是否梯队(0:否;1:是)

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "transfer_kind", referencedColumnName = "id")
    @Comment("调动性质")
    private Enumeration hrTransferKind; // 调动性质(1:平调;2:晋职;3:晋级;4:晋职晋级;5:支援;6:降级;7:降职;8:降职降级;9:转正;10:转正降级;11:转正降职;12:转正晋级;13:转正;14:兼职;15:试用期调薪;16:代理转正;17:代理转正晋级;18:待岗;19:试用期终止)

    @JSONField(format = DateUtil.SHOW_DATE_FORMAT)
    @Temporal(TemporalType.DATE)
    @Column(name = "handover_date")
    @Comment("交接日期")
    private Date handoverDate; //交接日期

    @JSONField(format = DateUtil.SHOW_DATE_FORMAT)
    @Column(name = "effective_date")
    @Comment("生效日期")
    private Date effectiveDate; //生效日期

    @Column(name = "end_date")
    @JSONField(format = DateUtil.SHOW_DATE_FORMAT)
    @Comment("支援结束日期")
    private Date endDate; //支援结束日期

    @Column(name = "role_list", length = 250)
    @Comment("本地角色备份")
    private String roleList; //本地角色备份 支援类型 下 调动 前将人员下所属有的岗位备份 到此处 ,新框架不将有支援还原表

    @Column(name = "icon_execute", nullable = false, columnDefinition = "number(1,0) default 0")
    @Comment("是否已执行")
    private boolean iconExecute = false; //是否已执行

    @ManyToOne(fetch=FetchType.LAZY,cascade=CascadeType.ALL)
    @JoinColumn(name = "transferApplication_id", referencedColumnName = "id",nullable = false)
    private TransferApplication transferApplications;

    @OneToMany(mappedBy="transferApplicationItems",cascade=CascadeType.ALL)
    private Set<TransferApplicationRole> transferApplicationRoles = new HashSet<>();
    public Set<TransferApplicationRole> getTransferApplicationRoles() {
        if (transferApplicationRoles == null) return null;
        for (TransferApplicationRole role: transferApplicationRoles) {
            role.setTransferApplicationItems(this);
        }
        return transferApplicationRoles;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = " repeal_uid", referencedColumnName = "id")
    private PartyRole repealUid; //终止人

    @Temporal(TemporalType.DATE)
    @JSONField(format = DateUtil.SHOW_DATE_FORMAT)
    @Column(name = " repeal_date")
    private Date  repealDate; //终止日期

    @Column(name = "repeal", nullable = false, columnDefinition = "number(1,0) default 0")
    @Comment("是否已终止")
    private boolean repeal = false; //是否已终止

    @Column(name = "repeal_cause", length = 1000)
    @Comment("终止原因")
    private String repealCause ; //终止原因

    @Column(name = "note", length = 250)
    @Comment("备注")
    private String note; //备注

    @Column(name = "distribution", columnDefinition = "number(1,0) default 0")
    @Comment("是否分配跟踪任务")
    private boolean distribution = false; //是否分配跟踪任务

    @Column(name = "salary", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    private double salary;    // 薪酬

    @Column(name = "salary_after", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    private double salaryAfter;    // 调动后薪酬

    @Column(name = "salary_effective_date")
    @JSONField(format = DateUtil.SHOW_DATE_FORMAT)
    @Comment("薪酬生效日期")
    private Date salaryEffectiveDate; //薪酬生效日期

    @Column(name = "actual_support_days", length = 50)
    @Comment("实际支援天数")
    private String actualSupportDays; //实际支援天数

    // 2021-05-26 支援单增加上下午 调动申请单不需要 AM为上午 PM为下午

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "effective_time",referencedColumnName = "id")
    @Comment("生效上下午时间")
    private Enumeration effectiveTime;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "end_time",referencedColumnName = "id")
    @Comment("结束上下午时间")
    private Enumeration endTime;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "repeal_time",referencedColumnName = "id")
    @Comment("终止上下午时间")
    private Enumeration repealTime;

}
